package com.zzvcom.webapp.action.admin;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.ProductFoundation;
import com.zzvcom.persist.model.admin.ProductKPSI;
import com.zzvcom.persist.model.admin.ProductPile;
import com.zzvcom.persist.model.admin.ProductQuality;
import com.zzvcom.persist.model.admin.ProductSize;
import com.zzvcom.service.admin.ProductManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class ProductAssitAction
{

    private ProductManager manager;

    private ExtMessage msg = new ExtMessage();// 返回信息

    private Map rs = new HashMap();// 结果集合

    private ResultPage resultPage;

    private Integer start;

    private Integer limit;

    public String query;

    public String size()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(ProductSize.class);
        if (StringUtils.isNotBlank(query))
        {
            dc.add(Restrictions.like("size", query, MatchMode.START));
        }
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }

    public String quality()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(ProductQuality.class);
        if (StringUtils.isNotBlank(query))
        {
            dc.add(Restrictions.like("quality", query, MatchMode.START));
        }
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }
    
    public String kpsi()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(ProductKPSI.class);
        if (StringUtils.isNotBlank(query))
        {
            dc.add(Restrictions.like("kpsi", query, MatchMode.START));
        }
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }
    
    public String foundation()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(ProductFoundation.class);
        if (StringUtils.isNotBlank(query))
        {
            dc.add(Restrictions.like("foundation", query, MatchMode.START));
        }
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }
    
    public String pile()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(ProductPile.class);
        if (StringUtils.isNotBlank(query))
        {
            dc.add(Restrictions.like("pile", query, MatchMode.START));
        }
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public ProductManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(ProductManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }

    public String getQuery()
    {
        return query;
    }

    public void setQuery(String query)
    {
        this.query = query;
    }

}
